//
//  BKNewsListViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/17.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKNewsListViewController.h"
#import "BKNewsTableViewCell.h"
#import "BKNewViiewModel.h"
#import <MBProgressHUD.h>
#import "BKNewsDetailViewController.h"

#define TYPE @[@"备考经验谈",@"招考动态",@"高考新闻眼",@"填报指导",@"自主招生",@"大学排名",@"专业排名",@"热点"]

@interface BKNewsListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthLayout;
@property (weak, nonatomic) IBOutlet UITableView *menuTableView;
@property (weak, nonatomic) IBOutlet UITableView *contentTableView;
@property (nonatomic ,copy) NSArray *dataArray;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@end


static  NSString *menuCell = @"menuCell";
static NSString *contentCell = @"ContentCell";
@implementation BKNewsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _widthLayout.constant = 0;
    _titleLabel.text = _typeName;
    
    [_menuTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:menuCell];
    UIView *backView = [UIView new];
    backView.backgroundColor = [UIColor colorWithWhite:0.369 alpha:1.000];
    _menuTableView.backgroundView = backView;
    
    [_contentTableView registerNib:[UINib nibWithNibName:NSStringFromClass([BKNewsTableViewCell class]) bundle:nil] forCellReuseIdentifier:contentCell];
    
    [self loadDataWithType:_type];
    
}
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)p_showMenu:(id)sender {
    
    
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.6 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseOut animations:^{
        _widthLayout.constant = _widthLayout.constant==0?150:0;
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
    
}


-(void)loadDataWithType:(NSString *)type{
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载";
    [self.view addSubview:hud];
    [hud removeFromSuperViewOnHide];
    [hud show:YES];
    _dataArray = @[];
    [_contentTableView reloadData];

    
    __weak BKNewsListViewController *weakSelf = self;
    [BKNewViiewModel newsWithType:type success:^(NSArray *dataArray) {
        [hud hide:YES];
        weakSelf.dataArray = dataArray;
        [weakSelf.contentTableView reloadData];
        
        weakSelf.widthLayout.constant = 0;
        
    } failure:^(NSString *message) {
        [hud hide:YES];
        weakSelf.widthLayout.constant = 0;
        
        UIAlertView *alert =[ [UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alert show];

    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if ([tableView isEqual:_contentTableView]) {
        BKNewsTableViewCell *newsCell = [tableView dequeueReusableCellWithIdentifier:contentCell];
        newsCell.titleLabel.text = _dataArray[indexPath.row][@"title"];
        newsCell.timeLabel.text = _dataArray[indexPath.row][@"time"];
        newsCell.fromLabel.text = _dataArray[indexPath.row][@"origin"];
        return newsCell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:menuCell];
    cell.textLabel.text = TYPE[indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if ([tableView isEqual:_menuTableView]) {
        return [UIView new];
    }
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if ([tableView isEqual:_menuTableView]) {
        return 40;
    }
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if ([tableView isEqual:_contentTableView]) {
        return  [_dataArray count];
    }
    return [TYPE count];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView isEqual:_contentTableView]) {
        return 100;
    }
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 10;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView isEqual:_menuTableView]) {
        _titleLabel.text = TYPE[indexPath.row];
        
       
        
            switch (indexPath.row) {
                case 5:
                    [self loadDataWithType:@"11"];
                    break;
                case 6:
                    [self loadDataWithType:@"21"];
                    break;
                case 7:
                    [self loadDataWithType:@"31"];
                    break;
                default:
                    [self loadDataWithType:[NSString stringWithFormat:@"%ld",indexPath.row+1]];
                    break;
           
                    
        }
       
        
      
    }
    
    if ([tableView isEqual:_contentTableView]) {
        BKNewsDetailViewController *detailVC = [[BKNewsDetailViewController alloc] initWithNibName:NSStringFromClass([BKNewsDetailViewController class]) bundle:nil];
        detailVC.articleId = _dataArray[indexPath.row][@"id"];
    
        detailVC.typeName = self.titleLabel.text;
        
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}
@end
